from rest_framework import serializers

from Problems.serializers import ProblemTagSerializer
from Submission.models import Submission


class SubmissionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Submission
        fields = (
            'id',
            'submission_time',
            'user',
            'problem',
            'contest',
            'code',
            'verdict',
            'lang',
            'time_cost',
            'memory_cost',
        )
        read_only_fields = ('id',)


class SubmissionListSerializer(serializers.ModelSerializer):
    problemId = serializers.IntegerField(source='problem.id')
    message = serializers.CharField(source='verdict')
    time = serializers.CharField(source='time_cost')
    memory = serializers.CharField(source='memory_cost')
    lang = serializers.CharField()
    createTime = serializers.DateTimeField(source='create_time')

    class Meta:
        model = Submission
        fields = (
            'problemId', 'message', 'time', 'memory', 'lang', 'createTime', 'remote_verdict')


class ResultSerializer(serializers.Serializer):
    time = serializers.IntegerField(required=False)  # 耗时（可选）
    memory = serializers.IntegerField(required=False)  # 内存（可选）


class SubmissionDetailListSerializer(serializers.ModelSerializer):
    _id = serializers.IntegerField(source='pk')
    username = serializers.CharField(source='user.username')
    personId = serializers.IntegerField(source='user.id')
    problemName = serializers.CharField(source='problem.title')
    problemId = serializers.IntegerField(source='problem.id')
    message = serializers.CharField(source='verdict')
    createTime = serializers.DateTimeField(source='create_time')
    lang = serializers.CharField()
    remoteVerdict = serializers.CharField(source='remote_verdict')
    tags = serializers.SerializerMethodField()

    def get_tags(self, obj):
        return ProblemTagSerializer(obj.recognized.all(), many=True).data

    def get_result(self, obj):
        return {
            'time': obj.time_cost or None,
            'memory': obj.memory_cost or None,
        }

    class Meta:
        model = Submission
        fields = (
            '_id',
            'tags',
            'personId',
            'username',
            'problemName',
            'problemId',
            'message',
            'lang',
            'createTime',
            'remoteVerdict',

        )

    # 自定义 `to_representation` 方法以处理 `result` 字段
    def to_representation(self, instance):
        representation = super().to_representation(instance)
        representation['result'] = self.get_result(instance)
        return representation


class SubmissionStatusDetail(serializers.ModelSerializer):
    username = serializers.CharField(source='user.username')
    message = serializers.CharField(source='verdict')
    createTime = serializers.DateTimeField(source='create_time')
    lang = serializers.CharField()
    remoteVerdict = serializers.CharField(source='remote_verdict')

    def get_result(self, obj):
        return {
            'time': obj.time_cost or None,
            'memory': obj.memory_cost or None,
        }

    class Meta:
        model = Submission
        fields = (
            'username',
            'code',
            'message',
            'lang',
            'createTime',
            'remoteVerdict'
        )

    # 自定义 `to_representation` 方法以处理 `result` 字段
    def to_representation(self, instance):
        representation = super().to_representation(instance)
        representation['result'] = self.get_result(instance)
        return representation
